package com.mingchuang.etc2.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import com.mingchuang.etc2.R;
import com.mingchuang.etc2.listener.SerialListener;
import com.mingchuang.etc2.serial.SerialCtrl;
import com.mingchuang.etc2.service.FloatingWindowService;
import com.mingchuang.etc2.util.ConvertUtil;
import com.mingchuang.etc2.util.PermissionUtil;

public class MainActivity extends AppCompatActivity {
    private Button openSerialPortBtn;
    private Button closeSerialPortBtn;
    private Button sendSerialDataBtn;
    private Button showFloatingWindowBtn;
    private SerialCtrl mSerialCtrl;

    private static final int OVERLAY_PERMISSION_CODE = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        initData();
    }

    private void initView() {
        openSerialPortBtn = findViewById(R.id.open_serial_port_btn);
        closeSerialPortBtn = findViewById(R.id.close_serial_port_btn);
        sendSerialDataBtn = findViewById(R.id.send_serial_port_btn);
        showFloatingWindowBtn=findViewById(R.id.show_floating_window_btn);
    }

    private void initEvent() {
        openSerialPortBtn.setOnClickListener(v -> {
            mSerialCtrl.openSerialPort("/dev/ttyS0", 115200);
        });
        closeSerialPortBtn.setOnClickListener(v -> {
            mSerialCtrl.close();
        });
        sendSerialDataBtn.setOnClickListener(v -> {
            mSerialCtrl.sendSerialData(ConvertUtil.hex2bytes("001122334455667788"));
        });
        showFloatingWindowBtn.setOnClickListener(v -> {
            initFloatingWindow();
        });
    }

    private void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OVERLAY_PERMISSION_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Settings.canDrawOverlays(this)) {
                showFloatingWindow();
            } else {
                Toast.makeText(this, "悬浮窗打开失败", Toast.LENGTH_SHORT).show();
            }
        }
    }


    /****************************************(分割线)****************************************/

    /**
     * 申请悬浮窗权限
     */

    private void initFloatingWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, OVERLAY_PERMISSION_CODE);
            } else {
                showFloatingWindow();
            }
        } else {
            showFloatingWindow();
        }
    }

    /**
     * 显示悬浮窗
     */
    private void showFloatingWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Log.i("LogMessage", "showFloatingWindow: A");
            startForegroundService(new Intent(this, FloatingWindowService.class));
        } else {
            Log.i("LogMessage", "showFloatingWindow: B");
            startService(new Intent(this, FloatingWindowService.class));
        }
    }

    /****************************************(串口)****************************************/

    /**
     * 初始化串口
     */
    private void initSerial() {
        mSerialCtrl = new SerialCtrl(new SerialListener() {
            @Override
            public void onSerialOpenSuccess(String path) {
                Log.i("LogMessage", "onSerialOpenSuccess: " + path);
            }

            @Override
            public void onSerialOpenException(String path, Exception e) {
                Log.i("LogMessage", "onSerialOpenException: " + e.getMessage());
            }

            @Override
            public void onSerialCloseSuccess(String path) {
                Log.i("LogMessage", "onSerialCloseSuccess: " + path);
            }

            @Override
            public void onReceiveData(byte[] data) {
                Log.i("LogMessage", "onReceiveData: " + ConvertUtil.bytes2hex(data));
            }

            @Override
            public void onSendData(byte[] data) {
                Log.i("LogMessage", "onSendData: " + ConvertUtil.bytes2hex(data));
            }
        });
    }
}